package com.controller;

import com.api.CommonResult;
import com.entity.table.LoginTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Login;
import com.service.LoginService;

import java.io.Serializable;
import java.util.List;

/**
 * 登录 控制层。
 *
 * @author admin
 * @since 2024-06-19
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping("login")
    public String toLoginPage() {return "client/login";}
    //登录
    @PostMapping("/toLogin")
    public String login(Login login, HttpSession httpSession, Model model) {
        QueryWrapper query = QueryWrapper.create()
                .where(LoginTableDef.LOGIN.PWD.eq(login.getUsername()))
                .and(LoginTableDef.LOGIN.PWD.eq(login.getPwd()));
        Login user = loginService.getOne(query);
        if (user != null) {
            httpSession.setAttribute("user", user);
            model.addAttribute("user", user);
//            return "client/index";
            return "redirect:/";
        }
        else    {
            model.addAttribute("msg", "账号或者密码错误，请重新输入！");
            return "client/login";
        }
    }


}
